// HSS.Forms.PanelGradient.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       PanelGradient.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System.ComponentModel;
	using System.Drawing;
	using System.Drawing.Drawing2D;
	using System.Windows.Forms;
	#endregion

	#region PanelGradient
	/// <summary>
	/// Summary description for PanelGradient.
	/// </summary>
	public partial class PanelGradient : System.Windows.Forms.Panel
	{
		
		/// <summary>
		/// Default Constructor
		/// </summary>
		public PanelGradient()
		{

			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// Activates double buffering 
			SetStyle(ControlStyles.UserPaint, true);
			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			SetStyle(ControlStyles.DoubleBuffer, true);

			// Forces Refresh on Resize
			SetStyle(ControlStyles.ResizeRedraw, true);
		}
		
		/// <summary>
		/// The High Color to use when painting the background
		/// </summary>
		[CategoryAttribute("Appearance")]
		public Color GradientHighColor
		{
			get { return _highColor; }
			set
			{
				_highColor = value;
				Invalidate();
			}
		} Color _highColor = SystemColors.ActiveCaption;

		/// <summary>
		/// The Low Color to use when painting the background
		/// </summary>
		[CategoryAttribute("Appearance")]
		public Color GradientLowColor
		{
			get { return _lowColor; }
			set
			{
				_lowColor = value;
				Invalidate();
			}
		} Color _lowColor = SystemColors.Control;

		/// <summary>
		/// If the Parent Control is Active or Not Active. If not active darkens the background to indicate as such.
		/// </summary>
		[CategoryAttribute("Hidden")]
		public bool Active
		{
			get { return _active; }
			set
			{
				_active = value;
				this.Invalidate(true);
			}
		} bool _active = true;

		/// <summary>
		/// The Gradient Mode for the Panel
		/// </summary>
		[CategoryAttribute("Appearance")]
		public LinearGradientMode GradientMode
		{
			get { return _horizontal; }
			set
			{
				_horizontal = value;
				this.Refresh();
			}
		} LinearGradientMode _horizontal = LinearGradientMode.Horizontal;

		/// <summary>
		/// Indicates whether or not to use GammaCorrection
		/// </summary>
		[CategoryAttribute("Appearance")]
		public bool UseGammaCorrection
		{
			get { return _useGammaCorrection; }
			set
			{
				_useGammaCorrection = value;
				this.Refresh();
			}
		} bool _useGammaCorrection = false;

		/// <summary>
		/// Paint the background.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
			LinearGradientBrush GBrush = null;
			try
			{
				if (_active)
					GBrush = new LinearGradientBrush(rect, _highColor, _lowColor, this.GradientMode);
				else
					GBrush = new LinearGradientBrush(rect, Color.DarkGray, Color.Gray, this.GradientMode);

				GBrush.GammaCorrection = _useGammaCorrection;
				e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
				e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
				e.Graphics.FillRectangle(GBrush, rect);
			}
			finally
			{
				if (null != GBrush)
				{
					GBrush.Dispose();
					GBrush = null;
				}
			}
			base.OnPaint(e);
		}
	}
	#endregion
}